package com.whnk.pm.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import com.whnk.common.constant.ConfigConstants;
import com.whnk.common.utils.file.FileUtils;
import com.whnk.pm.domain.FileInfo;
import com.whnk.pm.domain.ForbiddenStaff;
import com.whnk.pm.mapper.FileInfoMapper;
import com.whnk.pm.mapper.ForbiddenStaffMapper;
import com.whnk.pm.service.IForbiddenStaffService;
import com.whnk.pm.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.common.support.Convert;

/**
 * 禁用人员 服务层实现
 * 
 * @author whwhnk
 * @date 2019-09-27
 */
@Service
public class ForbiddenStaffServiceImpl implements IForbiddenStaffService
{
	@Autowired
	private ForbiddenStaffMapper forbiddenStaffMapper;

	@Autowired
	private FileInfoMapper fileInfoMapper;

	@Autowired
	private ConfigConstants configConstants;

	/**
     * 查询禁用人员信息
     * 
     * @param id 禁用人员ID
     * @return 禁用人员信息
     */
    @Override
	public ForbiddenStaff selectForbiddenStaffById(Integer id)
	{
	    return forbiddenStaffMapper.selectForbiddenStaffById(id);
	}
	
	/**
     * 查询禁用人员列表
     * 
     * @param forbiddenStaff 禁用人员信息
     * @return 禁用人员集合
     */
	@Override
	public List<ForbiddenStaff> selectForbiddenStaffList(ForbiddenStaff forbiddenStaff)
	{
	    return forbiddenStaffMapper.selectForbiddenStaffList(forbiddenStaff);
	}
	
    /**
     * 新增禁用人员
     * 
     * @param forbiddenStaff 禁用人员信息
     * @return 结果
     */
	@Override
	public int insertForbiddenStaff(ForbiddenStaff forbiddenStaff)
	{
	    return forbiddenStaffMapper.insertForbiddenStaff(forbiddenStaff);
	}
	
	/**
     * 修改禁用人员
     * 
     * @param forbiddenStaff 禁用人员信息
     * @return 结果
     */
	@Override
	public int updateForbiddenStaff(ForbiddenStaff forbiddenStaff)
	{
	    return forbiddenStaffMapper.updateForbiddenStaff(forbiddenStaff);
	}

	/**
     * 删除禁用人员对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteForbiddenStaffByIds(String ids)
	{
		return forbiddenStaffMapper.deleteForbiddenStaffByIds(Convert.toStrArray(ids));
	}

	@Override
	public void fileMove() {
		List<FileInfo> fileInfoList = fileInfoMapper.selectFileInfoList(null);
		for (FileInfo fileInfo : fileInfoList) {
			Integer fileId = fileInfo.getFileId();
			//从数据库查询文件数据
			FileInfo fileAttInfo = fileInfoMapper.selectFileAtt(String.valueOf(fileId));
			String uuid = UUID.randomUUID().toString();
			//保存图片
			String filePath = configConstants.getFileBaseDir() + uuid;
			FileUtils.saveFile(fileAttInfo.getEmpAtt(), filePath);

			fileAttInfo.setEmpAtt(null);
			fileAttInfo.setEmpFileId(uuid);
			fileInfoMapper.updateFileInfo(fileAttInfo);
		}
	}
}
